<template>
	<view class="content poRel">
		<view class="wid100 bacFFF">
			<view class="wid90 mar dis disAl disJuB" style="height: 116rpx;">
				<view class="fowe600 foSi30">是否接单</view>
				<view class="dis disAl">
					<view :style="jiedan?'color: '+theme.main_color+';':'color: #FF0000;'">{{jiedan?'接单':'不接单'}}</view>
					<switch :color="theme.main_color" style="transform:scale(0.7)" :checked="jiedan" @click="jiedan = !jiedan" />
				</view>
			</view>
		</view>
		<view class="wid100 mar-top20 bacFFF">
			<view class="wid90 mar dis disAl fowe600 foSi30" style="height: 100rpx;">选择接单时间</view>
			<view class="xia"></view>
			<view class="dis" style="height: 136rpx;">
				<picker class="hei100 dis disAl disJuC" style="width: 50%;border-right: 1rpx solid #E5E5E5;" mode="time"
					fields="month" :value="time" @change="bindTimeChange">
					<view class="textCen">
						<view class="foSi25 liH40">开始时间</view>
						<view class="fowe600 foSi30 mar-top20 liH40" :style="'color: '+theme.main_color+';'">{{time}}
						</view>
					</view>
				</picker>
				<picker class="hei100 dis disAl disJuC" style="width: 50%;" mode="time" fields="month" :value="time1"
					@change="bindTimeChange1">
					<view class="textCen">
						<view class="foSi25 liH40">结束时间</view>
						<view class="fowe600 foSi30 mar-top20 liH40" :style="'color: '+theme.main_color+';'">{{time1}}
						</view>
					</view>
				</picker>
			</view>
			<view class="xia"></view>
			<view class="wid90 mar mar-top30 foSi30" style="line-height: 40rpx;">
				<span class="mar-right10 fowe600">设置不可接单时间</span>
				<span class="foSi20" style="color: #737373;">（只能设置近5天的时间）</span>
			</view>
			<scroll-view class="wid90 mar mar-top20" style="white-space: nowrap;height: 84rpx;" scroll-x="true">
				<block v-for="(item,index) in timeList" :key="index">
					<view class="hei100" style="display: inline-block;"  @click="get_son_time(index)"
						:style="index+1 != timeList.length?'margin-right: 65rpx;':''">
						<view class="dis disAl disJuC hei80" :style="timeIndex == index?'color: '+theme.main_color+';':''">
							{{item.day_text}}</view>
						<view class="wid100" style="height: 4rpx;"
							:style="index == timeIndex?'background: '+theme.main_color+';':''"></view>
					</view>
				</block>
			</scroll-view>
		</view>

		<scroll-view style="height: calc(100vh - 547rpx);" class="wid100" scroll-y="true" @scrolltolower="lower">
				<view style="height: 10rpx;"></view>
				<view class="dis disAl flwa wid90 mar">
					<block v-for="(item, index) in yuyeList" :key="index">
						<view class="foSi25 mar-top20" style="width: 22%;"
							:style="(index+1) % 4 !== 0?'margin-right: 4%;':''">
							<view v-if="item.is_order != 1 && item.status != 1" class="textCen borRad20 bacFFF"
								style="padding: 20rpx 0;border: 1rpx solid #E5E5E5;" @click="timeClick(index)">
	<!-- :style="yuyeXuan == index?'background: '+theme.main_color+';color: #fff;border: 1rpx solid '+theme.main_color+';':'border: 1rpx solid #E5E5E5;'" -->
								<view class="foSi25" style="line-height: 40rpx;">{{item.time_text}}</view>
								<view class="foSi20">可预约</view>
							</view>
							<view v-if="item.is_order == 1 || item.status == 1" class="textCen borRad20"
								style="padding: 20rpx 0;color: #9B9B9B;border: 1rpx solid #9B9B9B;" @click="timeClick(index)">
								<view class="foSi25" style="line-height: 40rpx;">{{item.time_text}}</view>
								<view class="foSi20">不可预约</view>
							</view>
						</view>
					</block>
				</view>
				<view style="height: 130rpx;"></view>
			</scroll-view>
			<view class="wid100 poFix dis disAl disJuC bacFFF"
				style="box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0,0,0,0.2);height: 114rpx;bottom: 0;">
				<view @click="savetime" class="wid90 mar hei86 dis disAl disJuC coFFF foSi35 borRad40" :style="'background: '+theme.main_color+';'">
					保存</view>
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				jiedan: true,
				
				time: '00:00',
				time1: '23:59',
				timeList: [],
				timeIndex: 0,
				yuyeXuan: null,
				yuyeList: [],
				choice_t_index:0,
				theme: null
			}
		},
		onShow() {
			this.getdata()
		},
		onLoad() {
			this.theme = uni.getStorageSync('theme')
			this.getTime()
		},
		methods: {
			// 保存时间
			savetime(){
				let that = this
				let params = {
					rec_order:that.jiedan ? 1 : 0,
					start_time:that.time,
					end_time:that.time1,
					tech_id:uni.getStorageSync('tech_id'),
					date:that.timeList[that.timeIndex]['day_time'],
					timeinfo:JSON.stringify(that.yuyeList),
				}
				uni.showLoading({
					title:'加载中'
				})
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/tech/tech_set_time_set',
					method: "POST",
					header: {
						'site-id': that.global.site_id ,//自定义请求头信息
						
					},
					data:params,
					success(res) {
						let result = res.data
						if (result.code == 1) {
							uni.showToast({
								title:'保存成功'
							})
							
						}else{
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							}) 
						}
					},
					complete() {
						uni.hideLoading()
					}
					
				})
			},
			// 获取时间设置
			getdata(){
				let that = this
				let params = {
					tech_id:uni.getStorageSync('tech_id')
				}
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/tech/tech_get_time_set',
					method: "POST",
					header: {
						'site-id': that.global.site_id ,//自定义请求头信息
						
					},
					data:params,
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.time = result.data.start_time
							that.time1 = result.data.end_time
							that.jiedan = result.data.rec_order == 1 ? true : false
							that.timeList = result.data.times
							that.getall(0)
							console.log(that.jiedan)
						}else{
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							}) 
						}
					}
				})
			},
			
			get_son_time(index){
				
				console.log('嘿嘿嘿')
				let that = this
				let params = {
					tech_id:uni.getStorageSync('tech_id'),
					day_time:that.timeList[index]['day_time']
				}
				if(that.choice_t_index != index){
					
					uni.showModal({
					    title: '提示',
					    confirmText:'保存', //确定按钮的文字，默认为"确定"
					    cancelText:'取消',//取消按钮的文字，默认为"取消"
					    content: '是否保存当前时间设置？',
					    showCancel:true,//是否显示取消按钮，默认为 true
					    cancelColor:'#eeeeee',//取消按钮的文字颜色，默认为"#000000"
					    confirmColor:'#007aff',//确定按钮的文字颜色
						success: function (res) {
						    if (res.confirm) {
							    // 保存并重新获取
								that.getall(index)
							} else if (res.cancel) {
								that.getall(index)
							}
						}
					});
					
				}
				
			},
			getall(index){
				let that = this
				let params = {
					tech_id:uni.getStorageSync('tech_id'),
					day_time:that.timeList[index]['day_time']
				}
				that.choice_t_index = index
				that.timeIndex = index
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/technician/reservation_time',
					method: "POST",
					header: {
						'site-id': that.global.site_id ,//自定义请求头信息
						
					},
					data:params,
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.yuyeList = result.data
						}else{
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							}) 
						}
					}
				})
			},
			
			
			getTime() {
				// var check = [];
				// //获取当前年月日
				// var now = new Date();
				// for (let i = 0; i < 7; i++) {
				// 	//24 * 3600 * 1000 就是计算一天的时间  
				// 	var date = new Date(now.getTime() + i * 24 * 3600 * 1000);
				// 	var year = date.getFullYear();
				// 	var month = date.getMonth() + 1;
				// 	var day = date.getDate();
				// 	month = month >= 10 ? month : '0' + month
				// 	day = day >= 10 ? day : '0' + day
				// 	//把七天的时间和星期添加到数组中
				// 	if (i == 0) {
				// 		check = check.concat({
				// 			name: '今天',
				// 			time: year + '-' + month + '-' + day
				// 		})
				// 	} else {
				// 		check = check.concat({
				// 			name: month + '-' + day,
				// 			time: year + '-' + month + '-' + day
				// 		})
				// 	}
				// }
				// console.log(check)
				// this.timeList = check
			},
			timeClick(index) {
				
				console.log('设置内容',index)
				let c = this.yuyeList[index]['status']
				if(c == 1){
					this.yuyeList[index]['status'] = 0
				}else{
					this.yuyeList[index]['status'] = 1
				}
			},
			bindTimeChange: function(e) {
				this.time = e.detail.value
			},
			bindTimeChange1: function(e) {
				this.time1 = e.detail.value
			},
			getList() {

			},
			tiaozhuan(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
		background-color: #F3F1F1;
	}

	.xia {
		height: 1rpx;
		background-color: #E5E5E5;
	}

	.butt1 {
		width: 120rpx;
		height: 42rpx;
		color: #8A8A8A;
		border: 1rpx solid #8A8A8A;
		border-radius: 8rpx;
	}

	.butt2 {
		width: 120rpx;
		height: 42rpx;
		background-color: #00AD27;
		border: 1rpx solid #00AD27;
		border-radius: 8rpx;
	}
</style>